<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" v-auth="'RoleManagerPriv.Add'">
          新增角色
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              auth: 'RoleManagerPriv.PrivRange',
              icon: 'ion:git-compare-outline',
              onClick: handlePermission.bind(null, record),
            },
            {
              auth: 'RoleManagerPriv.Edit',
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              auth: 'RoleManagerPriv.Delete',
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <RoleModal @register="registerModal" @success="handleSuccess" />
    <MenuPermissionModal @register="permissionRegisterModel" />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { getRoleListByPage, deleteRole } from '/@/api/system/system';

  import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import RoleModal from './RoleModal.vue';
  import MenuPermissionModal from '../components/MenuPermissionModal.vue';

  import { columns, searchFormSchema } from './role.data';

  export default defineComponent({
    name: 'RoleManager',
    components: { BasicTable, TableAction, RoleModal, MenuPermissionModal },
    setup() {
      const { createMessage } = useMessage();
      const [registerTable, { reload }] = useTable({
        title: '角色列表',
        api: getRoleListByPage,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: undefined,
        },
      });

      function handleSuccess() {
        reload();
      }

      /***************************角色Modal***********************************/

      const [registerModal, { openModal }] = useModal();

      function handleCreate() {
        openModal(true, {
          isUpdate: false,
        });
      }

      function handleEdit(record: Recordable) {
        openModal(true, {
          record,
          isUpdate: true,
        });
      }

      async function handleDelete(record: Recordable) {
        await deleteRole(record.roleCode);
        createMessage.success('角色删除成功');
        reload();
      }

      /***************************权限范围Modal***********************************/

      const [permissionRegisterModel, permissionModalMethods] = useModal();

      function handlePermission(record: Recordable) {
        permissionModalMethods.openModal(true, { id: record.roleCode, type: 'R' });
      }

      return {
        registerTable,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        registerModal,
        permissionRegisterModel,
        handlePermission,
      };
    },
  });
</script>
